package autotd;

import java.util.ArrayList;
import java.util.List;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileNotFoundException;
import java.io.IOException;

public class MonsterType {

	public final String Name;
	public final int Health;
	public final int Height;
	public final int Delay;
	public final int Armour;
	public final int Reward;
	public final String[] Elements;

	public MonsterType(String name, int health, int height, int delay, int armour, int reward, String[] elements) {
		Name = name;
		Health = health;
		Height = height;
		Delay = delay;
		Armour = armour;
		Reward = reward;
		Elements = elements;
	}

	public String toString() {
		return Name + " " + Health + " " + Height + " " + Delay + " " + Armour + " " + Reward + print(Elements);
	}
  
	public int hashCode() {
		return toString().hashCode();
	}


	public static List<MonsterType> parseFile(File fname) throws FileNotFoundException, IOException {
		List<MonsterType> result = new ArrayList<MonsterType>();
		FileReader infile = new FileReader(fname);
		BufferedReader input = new BufferedReader(infile);
		String line = input.readLine();
		while (line != null) {
			if (line.indexOf("#") != -1) {
				line = line.substring(0, line.indexOf("#"));
			}
			if (line.length() > 0) {
				String[] tokens = line.split(" ");
				MonsterType mt = new MonsterType(tokens[0],
					Integer.parseInt(tokens[1]), Integer.parseInt(tokens[2]),
					Integer.parseInt(tokens[3]), Integer.parseInt(tokens[4]),
					Integer.parseInt(tokens[5]),
					repack(tokens, 6, tokens.length));
				result.add(mt);
			}
			line = input.readLine();
		}
		return result;
	}

	// start is included, end is not included
	public static String[] repack(String[] list, int start, int end) {
		String[] result = new String[end - start];
		for (int pos = start; pos < end; pos++) {
			result[pos - start] = list[pos];
		}
		return result;
	}

	private static String print(String[] list) {
		String result = "";
		if (list.length == 0) {
			return "";
		}
		for (String word : list) {
			result += " " + word;
		}
		return result;
	}
}